package handlers

import (
	"cargo-master/cache"
	"cargo-master/models"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func RopeAdd(c *gin.Context) {
	rope_no := c.PostForm("rope_no")
	rope_name := c.PostForm("rope_name")
	rope_type := c.PostForm("rope_type")
	rope_num := c.PostForm("rope_num")
	if rope_no == "" || rope_num == "" || rope_type == "" || rope_name == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Rope{
		RopeNo:   rope_no,
		RopeName: rope_name,
		RopeType: rope_type,
		RopeNum:  rope_num,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
		"data": data,
	})
	return
}
func OrderAdd(c *gin.Context) {
	order_no := c.PostForm("order_no")
	order_name := c.PostForm("order_name")
	order_price := c.PostForm("order_price")
	order_time := c.PostForm("order_time")
	if order_no == "" || order_name == "" || order_price == "" || order_time == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.RopeOrder{
		OrderNO:    order_no,
		OrderName:  order_name,
		OrderPrice: order_price,
		OrderTime:  order_time,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
		"data": data,
	})
	return
}
func RopeOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	/*
		SELECT
		  rope_orders.id AS ID,
		  ropes.rope_no AS RopeNo,
		  ropes.rope_name ASRopeName,
		  ropes.rope_type AS RopeType,
		  ropes.rope_num AS RopeNum,
		  rope_orders.order_no AS OrderNO,
		  rope_orders.order_name AS OrderName,
		  rope_orders.order_price AS OrderPrice,
		  rope_orders.order_time AS OrderTime
		FROM
		  rope_orders
		  LEFT JOIN ropes ON ropes.id = rope_orders.id
	*/
	var list []models.RopeOrderList
	redis_key := "rope_order_list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("rope_orders").
			Select(
				"rope_orders.id AS ID",
				"ropes.rope_no AS RopeNo",
				"ropes.rope_name ASRopeName",
				"ropes.rope_type AS RopeType",
				"ropes.rope_num AS RopeNum",
				"rope_orders.order_no AS OrderNO",
				"rope_orders.order_name AS OrderName",
				"rope_orders.order_price AS OrderPrice",
				"rope_orders.order_time AS OrderTime",
			).
			Joins("LEFT JOIN ropes ON ropes.id = rope_orders.id").
			Offset(offset).
			Limit(size).
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	var total int64
	models.DB.Model(models.RopeOrder{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  list,
		"total": total,
	})
	return
}
func RopeOrderDetail(c *gin.Context) {
	id := c.Query("id")
	/*
		SELECT
		  rope_orders.id AS ID,
		  ropes.rope_no AS RopeNo,
		  ropes.rope_name ASRopeName,
		  ropes.rope_type AS RopeType,
		  ropes.rope_num AS RopeNum,
		  rope_orders.order_no AS OrderNO,
		  rope_orders.order_name AS OrderName,
		  rope_orders.order_price AS OrderPrice,
		  rope_orders.order_time AS OrderTime
		FROM
		  rope_orders
		  LEFT JOIN ropes ON ropes.id = rope_orders.id
		  WHERE
		  rope_orders.id=1
	*/
	var list []models.RopeOrderList
	redis_key := "rope_order_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("rope_orders").
			Select(
				"rope_orders.id AS ID",
				"ropes.rope_no AS RopeNo",
				"ropes.rope_name ASRopeName",
				"ropes.rope_type AS RopeType",
				"ropes.rope_num AS RopeNum",
				"rope_orders.order_no AS OrderNO",
				"rope_orders.order_name AS OrderName",
				"rope_orders.order_price AS OrderPrice",
				"rope_orders.order_time AS OrderTime",
			).
			Joins("LEFT JOIN ropes ON ropes.id = rope_orders.id").
			Where("rope_orders.id=?", id).
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}
